package com.qf.shopping.dao.impl;

import com.qf.shopping.dao.ICartDao;
import com.qf.shopping.entity.ShoppingCart;
import com.qf.shopping.utils.Tools;

import java.util.List;

/**购物车与数据库的交互
 * @author 三旬
 * @version 1.0  2022/9/22
 */

public class CartDaoImpl implements ICartDao {

    @Override
    public Integer insert(ShoppingCart cart) {
        String sql = "insert into t_cart(id,pid,nums,uid) values(default,?,?,?)";
        return Tools.update(sql,cart.getPid(),cart.getNums(),cart.getUid());
    }

    @Override
    public Integer deleteById(Integer uid) {
        String sql = "delete from t_cart where uid = ?";
        return Tools.update(sql,uid);
    }

    @Override
    public Integer updateById(ShoppingCart shoppingCart) {
        return null;
    }

    @Override
    public ShoppingCart selectOneById(Integer id) {
        return null;
    }

    @Override
    public List<ShoppingCart> selectAll() {
        return null;
    }

    @Override
    public List<ShoppingCart> selectPage(Integer index, Integer size) {
        return null;
    }

    @Override
    public Integer counts() {
        return null;
    }
    /**
     * 查询购物车某个用户的商品列表
     * @param uid
     * @return
     */
    @Override
    public List<ShoppingCart> selectProductByUid(Integer uid) {
        String sql = "select * from t_cart where uid = ?";
        return Tools.selectList(sql,ShoppingCart.class,uid);
    }

    /**
     * 删除购物车的单个指定商品
     * @param pid
     * @param uid
     * @return
     */
    @Override
    public Integer deleteByPidAndUid(Integer pid, Integer uid) {
        String sql = "delete from t_cart where pid = ? and uid = ?";
        return Tools.update(sql,pid,uid);
    }

    @Override
    public ShoppingCart selectProductByPidAndUid(Integer pid, Integer uid) {
        String sql = "select * from t_cart where uid = ? and pid = ?";
        return Tools.selectOne(sql,ShoppingCart.class,uid,pid);
    }

    @Override
    public Integer updateNumsByPidAndUid(ShoppingCart cart) {
        String sql = "update t_cart set nums = ? where pid = ? and uid =?";
        return Tools.update(sql,cart.getNums(),cart.getPid(),cart.getUid());
    }

    @Override
    public ShoppingCart checkProductByUidAndPid(Integer uid, Integer pid) {
        String sql = "select *from t_cart where uid = ? and pid = ?";
        return Tools.selectOne(sql,ShoppingCart.class,uid,pid);
    }

    @Override
    public Integer addProductNumsByUidAndPid(ShoppingCart cart) {
        String sql = "update t_cart set nums = nums + ? where Uid = ? and Pid = ?";
        return Tools.update(sql,cart.getNums(),cart.getUid(),cart.getPid());
    }

    @Override
    public List<ShoppingCart> getCartProductNumsByUid(Integer uid) {
        String sql = "select * from t_cart where uid = ?";
        return Tools.selectList(sql,ShoppingCart.class,uid);
    }
}
